package com.zut.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zut.base.common.config.APIException;
import com.zut.base.common.entry.Pagination;
import com.zut.base.common.entry.Result;
import com.zut.base.entity.Project;
import com.zut.base.entity.Projectanduser;
import com.zut.base.entity.User;
import com.zut.base.mapper.ProjectMapper;
import com.zut.base.mapper.ProjectanduserMapper;
import com.zut.base.mapper.UserMapper;
import com.zut.base.service.IProjectanduserService;
import com.zut.base.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author yyl
 * @since 2021-03-31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ProjectanduserMapper projectanduserMapper;

    @Autowired
    private ProjectMapper projectMapper;

//    @Override
//    @Transactional(readOnly = true)
//    public Result<User> findAllMemberByPage(Pagination page, String search, Integer userId) throws RuntimeException {
//        Result<User> result = new Result<>();
//        //最终结果集
//        List<User> resultList = new ArrayList<>();
//        //封装全部符合条件的数据，使用set去重
//        Set<User> userSet = new HashSet<>();
//        QueryWrapper<Project> projectWrapper = new QueryWrapper<>();
//        if(userId!=null) {
//            projectWrapper.eq("createuserid", userId);
//        } else {
//            throw new APIException(408, "参数传入错误");
//        }
//        List<Project> projectList = projectMapper.selectList(projectWrapper);
//        if(projectList==null) {
//            projectList = new ArrayList<>();
//        }
//        for(Project project : projectList) {
//            QueryWrapper<Projectanduser> projectanduserWrapper = new QueryWrapper<>();
//            projectanduserWrapper.eq("projectid", project.getId());
//            projectanduserWrapper.eq("role", 2);
//            List<Projectanduser> projectandusers = projectanduserMapper.selectList(projectanduserWrapper);
//            if(projectandusers==null) {
//                projectandusers = new ArrayList<>();
//            }
//            for (Projectanduser projectanduser : projectandusers) {
//                QueryWrapper<User> userWrapper = new QueryWrapper<>();
//                userWrapper.eq("id", projectanduser.getUserid());
//                userWrapper.and(i->i.like("name", search).or().like("email", search));
//                List<User> users = userMapper.selectList(userWrapper);
//                userSet.addAll(users);
//            }
//        }
//        //开始手动分页
//        int total=0;
//        List<User> userList = new ArrayList<>(userSet);
//        total = userList.size();
//        //起始索引
//        int start = (page.getCurrent()-1)*page.getSize();
//        for(int i=start;i<total&&i<(start+page.getSize());i++) {
//            User user = userList.get(i);
//            resultList.add(user);
//        }
//        result.setList(userList);
//        result.setNum(total);
//        return result;
//    }

    @Override
    @Transactional(readOnly = true)
    public Result<Projectanduser> findAllMemberByPage(Pagination page, String search, Integer userId) throws RuntimeException {
        Result<Projectanduser> result = new Result<>();
        //分页后的结果集
        List<Projectanduser> resultList = new ArrayList<>();
        //封装全部符合条件的数据
        List<Projectanduser> projectanduserList = new ArrayList<>();
        QueryWrapper<Project> projectWrapper = new QueryWrapper<>();
        if(userId!=null) {
            projectWrapper.eq("createuserid", userId);
        } else {
            throw new APIException(408, "参数传入错误");
        }
        List<Project> projectList = projectMapper.selectList(projectWrapper);
        if(projectList==null) {
            projectList = new ArrayList<>();
        }
        for(Project project : projectList) {
            QueryWrapper<Projectanduser> projectanduserWrapper = new QueryWrapper<>();
            projectanduserWrapper.eq("projectid", project.getId());
            projectanduserWrapper.eq("role", 2);
            List<Projectanduser> projectandusers = projectanduserMapper.selectList(projectanduserWrapper);
            if(projectandusers==null) {
                projectandusers = new ArrayList<>();
            }
            for (Projectanduser projectanduser : projectandusers) {
                QueryWrapper<User> userWrapper = new QueryWrapper<>();
                userWrapper.eq("id", projectanduser.getUserid());
                userWrapper.and(i->i.like("name", search).or().like("email", search));
                List<User> users = userMapper.selectList(userWrapper);
                if(users!=null && users.size()!=0) {
                    projectanduser.setUser(userMapper.selectById(projectanduser.getUserid()));
                    projectanduser.setProject(projectMapper.selectById(projectanduser.getProjectid()));
                    projectanduserList.add(projectanduser);
                }
            }
        }
        //开始手动分页
        int total=0;
        total = projectanduserList.size();
        //起始索引
        int start = (page.getCurrent()-1)*page.getSize();
        for(int i=start;i<total&&i<(start+page.getSize());i++) {
            Projectanduser projectanduser = projectanduserList.get(i);
            resultList.add(projectanduser);
        }
        result.setList(resultList);
        result.setNum(total);
        return result;
    }
}